package com.ctshk.sapp.goods.controller;


import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.goods.dto.CommoditySpecificationsPageDTO;
import com.ctshk.rpc.goods.req.CommoditySpecificationsNameAddReq;
import com.ctshk.rpc.goods.req.CommoditySpecificationsNamePageReq;
import com.ctshk.rpc.goods.service.ICommoditySpecificationsNameService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 * 商品规格名称 前端控制器
 * </p>
 *
 * @author 姜斌文
 * @since 2021-04-12
 */
@Api(value="商品管理-商品规格", tags="商品管理-商品规格")
@RestController
@RequestMapping("/commodity/specifications/name")
public class CommoditySpecificationsNameController extends BaseController {
	@DubboReference
    private ICommoditySpecificationsNameService specificationsNameService;

    @ApiOperation(value = "新增商品规格名称", notes = "新增商品规格名称")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "新增商品规格名称", operDesc = "新增商品规格名称")
    @PostMapping("/addCommoditySpecificationsName")
    public Result addCommoditySpecificationsName(@RequestBody @Valid CommoditySpecificationsNameAddReq obj) throws Exception {
        try{
            specificationsNameService.addCommoditySpecificationsName(obj, getCurrentUser(getRequest()).getId());
        }catch (Exception e){
            return new Result<>(500, "名称已存在");
        }
        return Result.success();
    }

    @ApiOperation(value = "编辑商品规格名称", notes = "编辑商品规格名称")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "编辑商品规格名称", operDesc = "编辑商品规格名称")
    @PostMapping("/updateCommoditySpecificationsName")
    public Result updateCommoditySpecificationsName(@RequestBody @Valid CommoditySpecificationsNameAddReq obj) throws Exception {
    	specificationsNameService.updateCommoditySpecificationsName(obj,getCurrentUser(getRequest()).getId());
        return Result.success();
    }
    
    @ApiOperation(value = "分页查询商品规格名称管理", notes = "分页查询商品规格名称管理")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "分页查询商品规格名称管理", operDesc = "分页查询商品规格名称管理")
    @PostMapping("/findPageList")
    public Result<PageResponse<CommoditySpecificationsPageDTO>> findPageList(@RequestBody CommoditySpecificationsNamePageReq pageReq) {
        PageResponse<CommoditySpecificationsPageDTO> response = specificationsNameService.findCommoditySpecificationsName(pageReq,getCurrentUser(getRequest()));
        return Result.success(response);
    }
}
